package com.cndongtai.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.cndongtai.dao.BaseDao;
import com.cndongtai.dao.inter.UserMapper;
import com.cndongtai.po.User;

@Repository("userDao")
public class UserDao extends BaseDao implements UserMapper{
	
	@Override
	public User queryByUserId(Long userId) {
		
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper operation = sqlSession.getMapper(UserMapper.class);
			return operation.queryByUserId(userId);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}
		
		return null; 
	}

	@Override
	public void insertUser(User user) {
		SqlSession sqlSession = getSqlSession();
		
		try {
			UserMapper operation = sqlSession.getMapper(UserMapper.class);
			operation.insertUser(user);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}
		
		System.out.println("=================id="+user.getUserId()+"========================");
	}

	@Override
	public User queryByUsername(String username) {
		SqlSession sqlSession = getSqlSession();
		try {
			UserMapper operation = sqlSession.getMapper(UserMapper.class);
			return operation.queryByUsername(username);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}
		
		return null;
	}

	@Override
	public List<User> queryUserList() {
		SqlSession sqlSession = getSqlSession();
		
		try {
			UserMapper operation = sqlSession.getMapper(UserMapper.class);
			return operation.queryUserList();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}
		
		return null;
	}

	@Override
	public User queryByMobile(String mobile) {
		SqlSession sqlSession = getSqlSession();
		
		try {
			UserMapper operation = sqlSession.getMapper(UserMapper.class);
			return operation.queryByMobile(mobile);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}
		
		return null;
	}

	@Override
	public void updateUser(User user) {
		SqlSession sqlSession = getSqlSession();
		
		try {
			UserMapper operation = sqlSession.getMapper(UserMapper.class);
			operation.updateUser(user);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}
		
	}

	@Override
	public boolean deleteUser(User user) {
		SqlSession sqlSession = getSqlSession();
		
		try {
			UserMapper operation = sqlSession.getMapper(UserMapper.class);
			return operation.deleteUser(user);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			sqlSession.close();
		}
		
		return false;
	}

}
